package com.entfrm.biz.it.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entfrm.biz.it.entity.Property;
import com.entfrm.biz.it.entity.PropertyCategory;
import com.entfrm.biz.it.excel.PropertyExcel;
import com.entfrm.biz.it.excel.PropertyExcelVo;
import com.entfrm.biz.it.service.PropertyCategoryService;
import com.entfrm.biz.it.service.PropertyService;
import com.entfrm.core.base.api.R;
import com.entfrm.core.base.constant.CommonConstants;
import org.apache.poi.ss.formula.functions.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <h3>entfrm</h3>
 * <p>资产信息</p>
 *
 * @author : kyc
 * @date : 2020-08-08 08:49
 **/
@Component
public class PropertyManager {

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private PropertyCategoryService propertyCategoryService;

    /**
     * 查询资产列表
     */
    public IPage<Property> pageProperty(Page page, QueryWrapper<Property> queryWrapper) {
        IPage<Property> propertyPage = propertyService.page(page, queryWrapper);
        List<PropertyCategory> categoryList = propertyCategoryService.list();
        StringBuilder s = new StringBuilder();
        // 添加分类名称
        for (Property property : propertyPage.getRecords()) {
            if (property.getCategoryIds() != null) {
                String[] categoryIds = property.getCategoryIds().split(",");
                for (String categoryId : categoryIds) {
                    for (PropertyCategory category : categoryList) {
                        if (category.getId() == Integer.parseInt(categoryId)) {
                            s.append(category.getName());
                            s.append("/");
                        }
                    }
                }
            }
            for (PropertyCategory category : categoryList) {
                if (category.getId().equals(property.getCategoryId())) {
                    s.append(category.getName());
                }
            }
            property.setCategoryName(s.toString());
            s = new StringBuilder();
        }
        return propertyPage;
    }

    /**
     * 查询资产信息详情
     */
    public Property getPropertyById(Integer id) {
        Property property = propertyService.getById(id);
        List<PropertyCategory> categoryList = propertyCategoryService.list();
        StringBuilder s = new StringBuilder();
        if (property.getCategoryIds() != null) {
            String[] categoryIds = property.getCategoryIds().split(",");
            for (String categoryId : categoryIds) {
                for (PropertyCategory category : categoryList) {
                    if (category.getId() == Integer.parseInt(categoryId)) {
                        s.append(category.getName());
                        s.append("/");
                    }
                }
            }
        }
        for (PropertyCategory category : categoryList) {
            if (category.getId().equals(property.getCategoryId())) {
                s.append(category.getName());
            }
        }
        property.setCategoryName(s.toString());
        return property;
    }

    /**
     * 资产新增
     *
     * @return
     */
    public R saveProperty(Property property) {
        if (checkCowinNum(property)) {
            return R.error("凯翼编号重复,重新输入!");
        }
        PropertyCategory category = propertyCategoryService.getById(property.getCategoryId());
        if (0 != category.getParentId()) {
            property.setCategoryIds(category.getAncestors());
        } else {
            property.setCategoryIds("0");
        }
        // 新增状态在库
        property.setStatus(CommonConstants.PropertyStatus.IN_STOCK);
        propertyService.save(property);
        return R.ok();
    }

    /**
     * 资产修改
     *
     * @return
     */
    public R updatePropertyById(Property property) {
        if (checkCowinNum(property)) {
            return R.error("凯翼编号重复,重新输入!");
        }
        // 所属分类是否修改
        Property oldProperty = propertyService.getById(property.getId());
        if (!property.getCategoryId().equals(oldProperty.getCategoryId())) {
            PropertyCategory category = propertyCategoryService.getById(property.getCategoryId());
            if (0 != category.getParentId()) {
                property.setCategoryIds(category.getAncestors());
            } else {
                property.setCategoryIds("0");
            }
        }
        propertyService.updateById(property);
        return R.ok();
    }

    /**
     * 修改资产状态待报废
     */
    public void waitScrapProperty(Property property) {
        Property oldProperty = propertyService.getById(property.getId());
        if (oldProperty.getStatus().equals(CommonConstants.PropertyStatus.IN_STOCK)) {
            property.setStatus(CommonConstants.PropertyStatus.WAIT_SCRAP);
            propertyService.updateById(property);
        }
    }

    /**
     * 修改资产状态维修中
     */
    public void inServiceProperty(Property property) {
        Property oldProperty = propertyService.getById(property.getId());
        if (oldProperty.getStatus().equals(CommonConstants.PropertyStatus.IN_STOCK)) {
            property.setStatus(CommonConstants.PropertyStatus.IN_SERVICE);
            propertyService.updateById(property);
        }
    }

    /**
     * 修改资产状态在库(恢复正常)
     */
    public void inStockProperty(Property property) {
        Property oldProperty = propertyService.getById(property.getId());
        if (oldProperty.getStatus().equals(CommonConstants.PropertyStatus.WAIT_SCRAP)||
                oldProperty.getStatus().equals(CommonConstants.PropertyStatus.IN_SERVICE)) {
            property.setStatus(CommonConstants.PropertyStatus.IN_STOCK);
            propertyService.updateById(property);
        }
    }

    /**
     * 校验凯翼资产编号是否重复
     */
    public boolean checkCowinNum(Property property) {
        List<Property> propertyList = propertyService.list();
        for (Property oldProperty : propertyList) {
            if (property.getCowinNum().equals(oldProperty.getCowinNum())) {
                return !property.getId().equals(oldProperty.getId());
            }
        }
        return false;
    }

    public List<PropertyExcel> listPropertyExcel(Property property) {
        List<PropertyExcel> propertyExcelList = propertyService.listPropertyExcel(property);
        List<PropertyCategory> categoryList = propertyCategoryService.list();
        StringBuilder s = new StringBuilder();
        int index = 1;
        // 添加分类名称
        for (PropertyExcel propertyExcel : propertyExcelList) {
            if (propertyExcel.getCategoryIds() != null) {
                String[] categoryIds = propertyExcel.getCategoryIds().split(",");
                for (String categoryId : categoryIds) {
                    for (PropertyCategory category : categoryList) {
                        if (category.getId() == Integer.parseInt(categoryId)) {
                            s.append(category.getName());
                            s.append("/");
                        }
                    }
                }
            }
            for (PropertyCategory category : categoryList) {
                if (category.getId().equals(propertyExcel.getCategoryId())) {
                    s.append(category.getName());
                }
            }
            propertyExcel.setCategoryName(s.toString());
            s = new StringBuilder();
            propertyExcel.setIndex(index);
            index ++;
        }
        return propertyExcelList;
    }
}
